//
// Created by wuxiangan on 2022/4/18.
//

#ifndef LUI_THREADLOCAL_H
#define LUI_THREADLOCAL_H

#include "Common.h"
#include "ThreadMessage.h"

//namespace
class ThreadLocal {
public:
    ThreadLocal(std::string name = "");
    ~ThreadLocal();

    std_thread_id GetTid() { return m_tid; }
    std::string GetName() { return m_name; }
    void SetName(std::string name) { m_name = name; }

    void PushMessage(ThreadMessage* msg);
    ThreadMessage* PopMessage();
    std::vector<ThreadMessage*> PopAllMessage();
public:
    static ThreadLocal* GetThreadLocal();         // 获取当前线程的ThreadLocal
    static ThreadLocal* GetThreadLocalByName(std::string name);
    static ThreadLocal* GetThreadLocalById(std_thread_id tid);
    static std::unordered_map<std_thread_id, ThreadLocal*> s_thread_local_map;
    static std::mutex s_thread_local_map_mutex;
private:
    std_thread_id m_tid;
    std::string m_name;
    std::queue<ThreadMessage*> m_message_queue;
    std::mutex m_message_queue_mutex;
};

#endif //LUI_THREADLOCAL_H
